package configuracao;

//import hibernate.dao.HibernateConnection;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.sun.mail.handlers.image_jpeg;

import cursor.CursorController;
import email.EmailDAO;
import email.SendEmail;
import factory.FabricaDeFrame;
import principal.FrmPrincipal;
import skin.LookAndFeelSelector;

public class FrmUsuario implements MouseListener{

	private CtrlUsuario controle = new CtrlUsuario();
	//private FabricaDeFrame fabrica = new FabricaDeFrame();
	String mensagem;
	
	private JFrame frame;
	
	private JPanel panelPrincipal;
	private JPanel panelNorte;
	private JPanel panelLeste;
	private JPanel panelOeste;
	private JPanel panelSul;
	
	private JButton btnOk;
	private JButton btnCancelar;
	private JButton btnAdmin;
	
	private JTextField tfLogin;
	private JTextField tfSenha;

	private JLabel lblLogo;
	private JLabel lblTitulo;
	private JLabel lblLogin;
	private JLabel lblSenha;
	
	public FrmUsuario(){
		
		GridLayout gridBotao = new GridLayout(1,3);
		gridBotao.setHgap(10);
		
		panelPrincipal = new JPanel(new BorderLayout());
		panelNorte = new JPanel();
		panelLeste = new JPanel(new GridLayout(2,1));
		panelOeste = new JPanel(new GridLayout(2,1));
		panelSul = new JPanel(gridBotao);
		
		panelPrincipal.setBorder(new EmptyBorder(20, 10, 20, 10));
		panelNorte.setBorder(new EmptyBorder(10, 10, 10, 10));
		panelLeste.setBorder(new EmptyBorder(30, 10, 30, 10));
		panelOeste.setBorder(new EmptyBorder(30, 10, 30, 10));
		panelSul.setBorder(new EmptyBorder(10, 10, 10, 10));
		
		lblLogo = new JLabel();
		lblLogo.setIcon(new ImageIcon("./src/Repositorio/logo2.png"));
		
		panelNorte.add(lblLogo);
		
		lblLogin = new JLabel("Login: ");
		tfLogin = new JTextField("",25);
		lblSenha = new JLabel("Senha: ");
		tfSenha = new JTextField("",25);
		
		panelOeste.add(lblLogin);
		panelOeste.add(lblSenha);
		
		panelLeste.add(tfLogin);
		panelLeste.add(tfSenha);
		
		btnOk = new JButton("OK");
		btnCancelar = new JButton("Cancelar");
		btnAdmin = new JButton("Administrar");
		
		panelSul.add(btnOk);
		panelSul.add(btnAdmin);
		panelSul.add(btnCancelar);
		
		panelPrincipal.add(panelNorte, BorderLayout.NORTH);
		panelPrincipal.add(panelLeste, BorderLayout.EAST);
		panelPrincipal.add(panelOeste, BorderLayout.WEST);
		panelPrincipal.add(panelSul, BorderLayout.SOUTH);
		
		frame = new JFrame("Login de Usuario");
		frame.setSize(400, 350);
		frame.setContentPane(panelPrincipal);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		LookAndFeelSelector.lookAndFeelOS();
		frame.setVisible(true);
		
		MouseListener ml = this;
		ml = CursorController.createListener(frame, ml);
		
		btnOk.addMouseListener(ml);
		btnAdmin.addMouseListener(ml);
		btnCancelar.addMouseListener(ml);
		
		enviarEmailAniversariantes();
	}
	
	private void enviarEmailAniversariantes() {
		//controle.enviarEmail();
	    Thread threadDoPdf = new Thread(controle.enviarEmail());
	    threadDoPdf.start();
		
	}

	public static void main(String[] args){
		//HibernateConnection.init();
		new FrmUsuario();
	}

	public void encerrar(){
		JOptionPane.showMessageDialog(null, "Obrigado por utilizar o Salon Soft!");
		System.exit(0);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getComponent() == btnOk){
			try {
				mensagem = controle.verificarUsuario(tfLogin.getText(), tfSenha.getText());
			} catch (NoSuchAlgorithmException e1) {
				mensagem = e1.getMessage();
			}
			
			JOptionPane.showMessageDialog(null, mensagem);
			
			if("Bem vindo!".equals(mensagem)) {
				//fabrica.getInstancePrincipal();
				new FrmPrincipal();
				frame.dispose();
			}
		}
		
		if (e.getComponent() == btnAdmin){
			try {
				mensagem = controle.administrarUsuario(tfLogin.getText(), tfSenha.getText());
			} catch (NoSuchAlgorithmException e1) {
				mensagem = e1.getMessage();
			}
			
			JOptionPane.showMessageDialog(null, mensagem);
			
			if("Bem vindo!".equals(mensagem)) {
				new FrmUsuarioAdministrador();
				frame.dispose();
			}
				
		}
		
		if (e.getComponent() == btnCancelar){
			encerrar();
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		
	}

}
